"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx
import display_panel as dp


class ModEditor(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        
        self.parent = parent
        
        name = [
                    {'type' : 'text', 'desc' : 'Display name', 'key' : 'gui_name', 'tip' : 'The name used in the GUI.'},
                    {'type' : 'text', 'desc' : 'Internal name', 'key' : 'int_name', 'tip' : 'The name used internally by the game engine.', 'readonly' : True},
               ]
        
        files = [
                    {'type' : 'yes_no', 'desc' : 'Has package file', 'key' : 'package', 'tip' : 'Indicates the existence of a package file for this mod.', 'readonly' : True}
                ]
        
        save = [
                    {'type' : 'button', 'label' : 'Save', 'callback' : self.save}
               ]
        
        panel_list = [
                        {'type' : 'attributes', 'controls' : [name]},
                        {'type' : 'attributes', 'controls' : [files]},
                        {'type' : 'spacer'}
                     ]
        
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.disp = dp.DisplayPanel(self, panel_list, 7001)
        self.main_sizer.Add(self.disp, 1, wx.ALL | wx.EXPAND, border = 10)
        
        save_button = wx.Button(self, 7000, 'Save')
        self.Bind(wx.EVT_BUTTON, self.save, id=7000)
        self.main_sizer.Add(save_button, 0)
        
        self.SetSizer(self.main_sizer)
        
        
    def set_mod(self, mod):
        values = {'gui_name' : mod.get_friendly_name(), 'int_name' : mod.get_name(), 'package' : mod.has_package()}
        info = {'optional' : ['gui_name', 'int_name', 'package'], 'required': []}
        self.disp.set_module(info, values)
        
        self.mod = mod
    
    
    def evt_value_updated(self, key, value, from_child=True):
        pass
    
    
    def save(self, event):
        c = self.disp.get_changed_values()
        for k,v in c.iteritems():
            if k == 'gui_name':
                self.mod.package.set_value('config', 'uiname', v)
                self.mod.package.write_to_file()
                self.disp.module_values['gui_name'] = v     # As the mod reload isn't in yet, hack it to make sure we catch future updates
                # Should perform a full mod reload under the new name... current architecture doesn't allow this :(
    
